<?php
namespace app\admin\controller;
use app\common\model\Permission as PermissionModel;
use think\Request;
use think\Loader;

class Permission extends Base{

    /**
     *
     * @param Request $request
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index(Request $request){
        $url = $request->get('q');


        $where = [];
        if($url){
            $where = ['url'=>['like','%'.$url."%"]];
        }

        $model= PermissionModel::where($where)->paginate(10);

        $this->data['model'] = $model;
        $this->data['column'] = $this->column();
        $this->data['title'] = "权限管理";

        return view('',$this->data);
    }


    public function add(){
        $this->data['title'] = '权限添加';
        return view('',$this->data);
    }


    public function edit(Request $request){
        $this->data['title'] = '栏目更新';

        $this->data['model'] = PermissionModel::get($request->param('id'));

        return view('',$this->data);
    }


    /**
     * @param Request $request
     */
    public function handle(Request $request){

        $data = $request->post();

        $validate = Loader::validate('Permission');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        if(empty($data['id'])){
            if(PermissionModel::create($data)){
                $this->success("新增成功");
            }
        }else{
            $navigationModel = new PermissionModel();
            if($navigationModel -> save($data,['id'=>$data['id']])){
                $this->success("更新成功");
            }
        }

        $this->error("操作失败");
    }


    public function delete(Request $request){
        $model = PermissionModel::get($request->param('id'));
        if($model){
            $res = $model ->delete();
            if($res){
                $this->success("删除成功");
            }
        }

        $this->error("操作失败");
    }



    protected function column(){
        return [
            'title'=>'链接名称',
            'url'=>'权限标志',
            'group'=>'所属分组'
        ];
    }



}